package org.iks.gfs.business.controller;


import org.iks.common.AjaxResult;
import org.iks.gfs.business.service.ChunkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * Author kzou
 * Date 2024/2/14
 * Description: 文件服务
 */
@RequestMapping("chunk")
@RestController
public class ChunkController {

    @Autowired
    private ChunkService chunkService;

    @PostMapping("/upload/token")
    public AjaxResult<?> tokeChunkToken(@RequestParam String fileName) {
        String token = chunkService.chunkFileToken(fileName);
        return AjaxResult.success(token);
    }

    @PostMapping("upload")
    public AjaxResult<?> chunkUpload(
            MultipartFile file,
            @RequestParam("index") Integer index,
            @RequestParam("chunkToken") String chunkToken,
            @RequestParam(value = "chunkName", required = false) String chunkName,
            @RequestParam("fileName") String fileName) throws IOException {
        List<String> chunkFiles = chunkService.chunkUpload(file, index, fileName, chunkName, chunkToken);
        return AjaxResult.success(chunkFiles);
    }

    @PostMapping("merge")
    public AjaxResult<?> mergeChunkFile(
            @RequestParam("fileName") String fileName,
            @RequestParam(value = "chunkFiles", required = false) List<String> chunkFiles) {
        String gfsFileName = chunkService.mergeChunkFile(fileName, chunkFiles);
        return AjaxResult.success(gfsFileName);
    }

}
